package by.bsu.rfe.belianko.euniversity.controller.helper;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import by.bsu.rfe.belianko.euniversity.controller.command.AdminCommand;
import by.bsu.rfe.belianko.euniversity.controller.command.ApproveCommand;
import by.bsu.rfe.belianko.euniversity.controller.command.CabinetCommand;
import by.bsu.rfe.belianko.euniversity.controller.command.Command;
import by.bsu.rfe.belianko.euniversity.controller.command.CompleteCommand;
import by.bsu.rfe.belianko.euniversity.controller.command.EnrollCommand;
import by.bsu.rfe.belianko.euniversity.controller.command.LoginCommand;
import by.bsu.rfe.belianko.euniversity.controller.command.LogoutCommand;
import by.bsu.rfe.belianko.euniversity.controller.command.NoCommand;
import by.bsu.rfe.belianko.euniversity.controller.command.RegisterCommand;
import by.bsu.rfe.belianko.euniversity.controller.command.SaveCommand;

/**
 * 
 * 
 * @author Vitali_Belianko
 * 
 */
public class RequestHelper {
	private static final String APPROVE = "approve";
	private static final String COMPLETE = "complete";
	private static final String ENROLL = "enroll";
	private static final String ADMIN = "admin";
	private static final String LOGOUT = "logout";
	private static final String SAVE = "save";
	private static final String CABINET = "cabinet";
	private static final String COMMAND = "command";
	private static final String LOGIN = "login";
	private static final String REGISTER = "register";
	private static RequestHelper instance;
	private static final Map<String, Command> commands = new HashMap<String, Command>();
	static {
		commands.put(LOGIN, new LoginCommand());
		commands.put(CABINET, new CabinetCommand());
		commands.put(REGISTER, new RegisterCommand());
		commands.put(SAVE, new SaveCommand());
		commands.put(LOGOUT, new LogoutCommand());
		commands.put(ADMIN, new AdminCommand());
		commands.put(ENROLL, new EnrollCommand());
		commands.put(COMPLETE, new CompleteCommand());
		commands.put(APPROVE, new ApproveCommand());
	}

	public static RequestHelper getInstance() {
		if (instance == null) {
			instance = new RequestHelper();
		}
		return instance;
	}

	/**
	 * Return command by request parameter.
	 * 
	 * @param request
	 *            - ServletRequest
	 * @return command or no command if command=null
	 */
	public Command getCommand(HttpServletRequest request) {
		Command command;
		command = commands.get(request.getParameter(COMMAND));
		if (command == null) {
			command = new NoCommand();
		}
		return command;
	}

}
